package cn.com.gas.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.com.gas.mapper.GasStationinfoMapper;
import cn.com.common.constants.Constants;
import cn.com.common.exception.BusinessException;
import cn.com.common.exception.code.BaseResponseCode;
import cn.com.gas.entity.GasStationDeptEntity;
import cn.com.gas.entity.GasStationinfoEntity;
import cn.com.gas.service.GasStationDeptService;
import cn.com.gas.service.GasStationinfoService;
import cn.com.gas.vo.res.GasStationinfoRespNodeVO;
import cn.com.system.service.impl.HttpSessionService;
import cn.com.util.TsBqUtils;

@Service("gasStationinfoService")
@Transactional
public class GasStationinfoServiceImpl extends ServiceImpl<GasStationinfoMapper, GasStationinfoEntity>
		implements GasStationinfoService {

	@Autowired
	private GasStationinfoMapper gasStationinfoMapper;
	@Autowired
	private HttpSessionService httpSessionService;
	@Autowired
	private GasStationDeptService gasStationDeptService;

	// TODO
	@Override
	public List<GasStationinfoRespNodeVO> treeList(String id) {

		List<GasStationinfoEntity> list = null;
		String currentDeptId = httpSessionService.getCurrentDeptId();
		LambdaQueryWrapper<GasStationDeptEntity> queryWrapperStationDept = Wrappers.lambdaQuery();
		List<GasStationDeptEntity> listStationDept = null;
		if (StringUtils.isEmpty(id)) {
			String taxOrgLevel = TsBqUtils.getTaxOrgLevel(currentDeptId);
			
			if (Constants.TAX_COUNTES.equals(taxOrgLevel)) {
				// 县区局
				
				queryWrapperStationDept.likeRight(GasStationDeptEntity::getDeptId, currentDeptId.substring(0, 7));
				listStationDept = gasStationDeptService.list(queryWrapperStationDept);
				
				List<String> listRegionId = new ArrayList<>();
				List<String> listEndangerId = new ArrayList<>();
				
				for (GasStationDeptEntity gasStationDeptEntity : listStationDept) {
					listEndangerId.add(gasStationDeptEntity.getEndangerId());
					listRegionId.add(gasStationDeptEntity.getRegionId());
					
				}
				
				// 危化企业  
				LambdaQueryWrapper<GasStationinfoEntity> lambdaQuery = Wrappers.lambdaQuery();
				lambdaQuery.in(GasStationinfoEntity::getPid,listEndangerId);
				lambdaQuery.in(GasStationinfoEntity::getIsAnalyse,"Y");
				
				List<GasStationinfoEntity> _list = gasStationinfoMapper.selectList(lambdaQuery);
				List<String> stationinfoIds = new ArrayList<>();
				for (GasStationinfoEntity entity : _list) {
					stationinfoIds.add(entity.getId());
					
				}
				
				LambdaQueryWrapper<GasStationinfoEntity> lambdaQuery1 = Wrappers.lambdaQuery();
				lambdaQuery1.in(GasStationinfoEntity::getId,listEndangerId);
				List<GasStationinfoEntity> _list1 = gasStationinfoMapper.selectList(lambdaQuery1);
				// 区域
				LambdaQueryWrapper<GasStationinfoEntity> lambdaQuery2 = Wrappers.lambdaQuery();
				lambdaQuery2.in(GasStationinfoEntity::getId,listRegionId);
				List<GasStationinfoEntity> _list2 = gasStationinfoMapper.selectList(lambdaQuery2);
				
				String pid = _list2.get(0).getPid();
				
				GasStationinfoEntity gasStationinfoEntity = gasStationinfoMapper.selectById(pid);
				gasStationinfoEntity.setPid("0");
				
				LambdaQueryWrapper<GasStationinfoEntity> lambdaQuery3 = Wrappers.lambdaQuery();
				lambdaQuery3.in(GasStationinfoEntity::getPid,stationinfoIds);
				List<GasStationinfoEntity> _list3 = gasStationinfoMapper.selectList(lambdaQuery3);

				
				list = new ArrayList<>();
				for (GasStationinfoEntity entity : _list) {
					list.add(entity);
				}
				
				for (GasStationinfoEntity entity : _list1) {
					list.add(entity);
				}

				for (GasStationinfoEntity entity : _list2) {
					list.add(entity);
				}
				for (GasStationinfoEntity entity : _list3) {
					list.add(entity);
				}
				
				list.add(gasStationinfoEntity);
				
			} else if (Constants.TAX_CITIES.equals(taxOrgLevel)) {
				// 市局 
				
				queryWrapperStationDept.likeRight(GasStationDeptEntity::getDeptId, currentDeptId.substring(0, 5));
				listStationDept = gasStationDeptService.list(queryWrapperStationDept);
				
				List<String> listRegionId = new ArrayList<>();
				List<String> listEndangerId = new ArrayList<>();
				
				for (GasStationDeptEntity gasStationDeptEntity : listStationDept) {
					listEndangerId.add(gasStationDeptEntity.getEndangerId());
					listRegionId.add(gasStationDeptEntity.getRegionId());
					
				}
				
				// 危化企业  
				LambdaQueryWrapper<GasStationinfoEntity> lambdaQuery = Wrappers.lambdaQuery();
				lambdaQuery.in(GasStationinfoEntity::getPid,listEndangerId);
				lambdaQuery.in(GasStationinfoEntity::getIsAnalyse,"Y");
				
				List<GasStationinfoEntity> _list = gasStationinfoMapper.selectList(lambdaQuery);
				List<String> stationinfoIds = new ArrayList<>();
				for (GasStationinfoEntity entity : _list) {
					stationinfoIds.add(entity.getId());
					
				}
				
				
				LambdaQueryWrapper<GasStationinfoEntity> lambdaQuery1 = Wrappers.lambdaQuery();
				lambdaQuery1.in(GasStationinfoEntity::getId,listEndangerId);
				List<GasStationinfoEntity> _list1 = gasStationinfoMapper.selectList(lambdaQuery1);
				// 区域
				LambdaQueryWrapper<GasStationinfoEntity> lambdaQuery2 = Wrappers.lambdaQuery();
				lambdaQuery2.in(GasStationinfoEntity::getId,listRegionId);
				List<GasStationinfoEntity> _list2 = gasStationinfoMapper.selectList(lambdaQuery2);
				
				String pid = _list2.get(0).getPid();
				
				GasStationinfoEntity gasStationinfoEntity = gasStationinfoMapper.selectById(pid);
				gasStationinfoEntity.setPid("0");
				
				
				LambdaQueryWrapper<GasStationinfoEntity> lambdaQuery3 = Wrappers.lambdaQuery();
				lambdaQuery3.in(GasStationinfoEntity::getPid,stationinfoIds);
				List<GasStationinfoEntity> _list3 = gasStationinfoMapper.selectList(lambdaQuery3);

				list = new ArrayList<>();
				for (GasStationinfoEntity entity : _list) {
					list.add(entity);
				}
				
				for (GasStationinfoEntity entity : _list1) {
					list.add(entity);
				}

				for (GasStationinfoEntity entity : _list2) {
					list.add(entity);
				}
				
				for (GasStationinfoEntity entity : _list3) {
					list.add(entity);
				}
				
				list.add(gasStationinfoEntity);
				
				
				
			} else if (Constants.TAX_PROVINCES.equals(taxOrgLevel)) {
				// 省局
				
				listStationDept = gasStationDeptService.list(queryWrapperStationDept);
				
				list = gasStationinfoMapper.selectList(Wrappers.emptyWrapper());
				
			}

			
		} else {
			GasStationinfoEntity gasStationinfoEntity = gasStationinfoMapper.selectById(id);
			if (gasStationinfoEntity == null) {
				throw new BusinessException(BaseResponseCode.DATA_ERROR);
			}
			LambdaQueryWrapper<GasStationinfoEntity> queryWrapper = Wrappers.<GasStationinfoEntity>lambdaQuery()
					.likeRight(GasStationinfoEntity::getDomain, gasStationinfoEntity.getDomain());

			List<Object> childIds = gasStationinfoMapper.selectObjs(queryWrapper);
			list = gasStationinfoMapper.selectList(
					Wrappers.<GasStationinfoEntity>lambdaQuery().notIn(GasStationinfoEntity::getId, childIds));
		}

		if (TsBqUtils.isNotNull(listStationDept)) {
			// 处理list
			List<String> listStation = new ArrayList<>();
			for (GasStationDeptEntity stationDept : listStationDept) {
				listStation.add(stationDept.getEndangerId());
				listStation.add(stationDept.getRegionId());
			}

			// 默认加一个顶级方便新增顶级部门
			GasStationinfoRespNodeVO gasStationinfoRespNodeVO = new GasStationinfoRespNodeVO();
			gasStationinfoRespNodeVO.setTitle("默认顶级");
			gasStationinfoRespNodeVO.setId("0");
			gasStationinfoRespNodeVO.setSpread(true);
			gasStationinfoRespNodeVO.setDisabled(true);
			gasStationinfoRespNodeVO.setChildren(getTree(list, listStation));
			List<GasStationinfoRespNodeVO> result = new ArrayList<>();
			result.add(gasStationinfoRespNodeVO);
			return result;
		} else {
			return null;
		}

	}

	private List<GasStationinfoRespNodeVO> getTree(List<GasStationinfoEntity> all, List<String> listStation) {
		List<GasStationinfoRespNodeVO> list = new ArrayList<>();
		for (GasStationinfoEntity gasStationinfo : all) {
			if ("0".equals(gasStationinfo.getPid())) {
				// 处理名称
				if (gasStationinfo.getType().equals("group") || gasStationinfo.getType().equals("domain")) {
					gasStationinfo.setName(TsBqUtils.handleEndBeforeParentheses(gasStationinfo.getName()));
				}

				GasStationinfoRespNodeVO tree = new GasStationinfoRespNodeVO();

				BeanUtils.copyProperties(gasStationinfo, tree);

				tree.setTitle(gasStationinfo.getName());
				tree.setSpread(true);
				tree.setChildren(getChild(gasStationinfo.getId(), all, listStation));
				list.add(tree);
			}
		}

		return list;
	}

	private List<GasStationinfoRespNodeVO> getChild(String id, List<GasStationinfoEntity> all,
			List<String> listStation) {
		List<GasStationinfoRespNodeVO> list = new ArrayList<>();
		for (GasStationinfoEntity gasStationinfo : all) {
			if (gasStationinfo.getPid().equals(id)) {
				String isAnalyse = gasStationinfo.getIsAnalyse();

				if ("Y".equals(isAnalyse) || listStation.contains(gasStationinfo.getId())) {
				// if (true) {
					// 处理名称
					if (gasStationinfo.getType().equals("group") || gasStationinfo.getType().equals("domain")) {
						gasStationinfo.setName(TsBqUtils.handleEndBeforeParentheses(gasStationinfo.getName()));
					}

					GasStationinfoRespNodeVO tree = new GasStationinfoRespNodeVO();

					BeanUtils.copyProperties(gasStationinfo, tree);
					tree.setTitle(gasStationinfo.getName());
					tree.setChildren(getChild(gasStationinfo.getId(), all, listStation));
					list.add(tree);
				}

			}
		}
		return list;
	}

	@Override
	public void updateCurrentAndNextLevelById(GasStationinfoEntity gasStationinfo) {

		Date date = new Date();
		String currentUserId = httpSessionService.getCurrentUserId();
		gasStationinfo.setUpdateId(currentUserId);
		gasStationinfo.setUpdateTime(date);
		this.updateById(gasStationinfo);

		LambdaQueryWrapper<GasStationinfoEntity> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(GasStationinfoEntity::getPid, gasStationinfo.getId());
		// 更新下一级的 taxorgcode
		List<GasStationinfoEntity> list = this.list(queryWrapper);
		for (GasStationinfoEntity gasStationinfoEntity : list) {
			gasStationinfoEntity.setTaxorgcode(gasStationinfo.getTaxorgcode());
			gasStationinfoEntity.setUpdateId(currentUserId);
			gasStationinfoEntity.setUpdateTime(date);
			this.updateById(gasStationinfoEntity);

		}

	}

	@Override
	public void realRemoveById(String id) {
		gasStationinfoMapper.realRemoveById(id);
	}

}